package com.deft.design.observer.event;

import com.deft.design.observer.LotteryResult;
import com.deft.design.observer.event.listener.EventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hide丶love
 * @date 2022/05/18
 **/
public class EventManager {

    Map<Enum<EventType>, List<EventListener<LotteryResult>>> listenerMap = new HashMap<>();

    @SafeVarargs
    public EventManager(Enum<EventType>... operators) {
        for (Enum<EventType> operator : operators) {
            this.listenerMap.put(operator, new ArrayList<>());
        }
    }

    public enum EventType {
        /**
         * 消息枚举
         */
        MQ,
        MESSAGE
    }

    public void subscribe(Enum<EventType> eventType, EventListener<LotteryResult> listener) {
        List<EventListener<LotteryResult>> users = listenerMap.get(eventType);
        users.add(listener);
    }

    public void unsubscribe(Enum<EventType> eventType, EventListener<LotteryResult> listener) {
        List<EventListener<LotteryResult>> users = listenerMap.get(eventType);
        users.remove(listener);
    }

    public void notify(Enum<EventType> eventType, LotteryResult result) {
        List<EventListener<LotteryResult>> users = listenerMap.get(eventType);
        for (EventListener<LotteryResult> user : users) {
            user.doDraw(result);
        }
    }
}
